package com.gitee.ywj1352.dubbo.resilience4j.filter;

import com.gitee.ywj1352.dubbo.resilience4j.strategy.TryStrategy;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.rpc.*;

@Activate(group = CommonConstants.CONSUMER, after = "future")
public class FuseFilter implements Filter, Filter.Listener {


    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        System.out.println(" FuseFilter 执行 楼");
        URL url = invoker.getUrl();
        String methodName = invocation.getMethodName();
        String fallBackStrategy = url.getMethodParameter(methodName, "fallBackStrategy");
        if (fallBackStrategy == null) {
            return invoker.invoke(invocation);
        }
        ExtensionLoader<TryStrategy> tryStrategy = ExtensionLoader.getExtensionLoader(TryStrategy.class);
        TryStrategy extension = tryStrategy.getExtension(fallBackStrategy);
        return extension.getResult(invoker,invocation).get();
    }

    @Override
    public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
        //do something

    }

    @Override
    public void onError(Throwable t, Invoker<?> invoker, Invocation invocation) {
        //do something

    }
}
